
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;




public class DialogBox 
{

	
	public static void main(String[] args)
	{
		final JFrame fcframe = new JFrame(); 
		final JTextField t1 = new JTextField(5);
		final JTextField t2= new JTextField(20);
		final JTextField t3= new JTextField(20);
		final JTextField t4 = new JTextField(20);
		
		
		
		class OpenCloseListener implements ActionListener
		{
			
			
	        public void actionPerformed(ActionEvent evt) 
	        {
	            JFileChooser chooser = (JFileChooser)evt.getSource();
	            
	            
	            if (JFileChooser.APPROVE_SELECTION.equals(evt.getActionCommand())) {
	                // Open or Save was clicked
	            	File f = chooser.getSelectedFile();
	            	String s = f.getPath();
	            	if(chooser.getApproveButtonText().equals("Browse"))
	            		t1.setText(s);
	            	else if(chooser.getApproveButtonText().equals("Browse Me"))
	            		t3.setText(s);
	           	
	            	else if(chooser.getApproveButtonText().equals("Create Me"))
	            		t2.setText(s);
	           	
	            	// Hide dialog
	                fcframe.setVisible(false);
	               
	            } else if (JFileChooser.CANCEL_SELECTION.equals(evt.getActionCommand())) {
	                // Cancel was clicked
	    
	                // Hide dialog
	                fcframe.setVisible(false);
	            }
	            
	        }

		}
		
		
				
		class BrowseListener implements ActionListener
		{
			ActionListener cancellist = null;
			JFileChooser chooser = null;
			JPanel fcpan = null;
			
			public BrowseListener()
			{
				cancellist = new OpenCloseListener();
				chooser = new JFileChooser("C:\\Users\\vanand\\MyDocuments\\Thesis Work\\LD Experiment");
			    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
				chooser.addActionListener(this.cancellist);
				fcpan = new JPanel();
				
			
			}

			public void actionPerformed(ActionEvent event) 
			{
				JButton b = (JButton)event.getSource();
				if (b.getText().contains("Browse")|| b.getText().contains("Create"))
				{
			   	chooser.setApproveButtonText(b.getText());
			    fcpan.add(chooser);
				fcframe.add(fcpan);
				fcframe.setSize(600, 370);
				fcframe.setVisible(true);
				
				}
				
			}
		}
		
		class RunListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				JButton b = (JButton) event.getSource();
				
				
				try{ 
						
						File f = new File(t1.getText());
						
						if (f.isDirectory()) {
							
						}
					    else {
					    	
					    	String inputfile = t1.getText();
					    	String outputfile = t2.getText();
					    	String prevalancefile = t3.getText();
					    	String partialCorrelationThreshold = t4.getText();
					    	
					    	if(b.getText().equals("Run"))
							{		
							
					    		NoisyHybridBean nhb = new NoisyHybridBean();
					    		//nhb.processRequest(args[0], args[1], args[2], Double.valueOf(args[3]));
					    		nhb.processRequest(inputfile, outputfile, prevalancefile, Double.valueOf(partialCorrelationThreshold));
							}
					    	else if(b.getText().equals("Count"))
					    	{
					    		NoisyORBean norb = new NoisyORBean();
					    		norb.countParents(inputfile);
					    	}
					    }
				   	
					
					} catch (Exception e){
						e.printStackTrace();
					}
				
			}
		
			 
		}
			

			
		
		JFrame frame = new JFrame();
		JLabel infilename = new JLabel("Input Filename: ");
		JLabel outfilename = new JLabel("Output Filename: ");
		JLabel prevfilename = new JLabel("Prevalance Filename: ");
		JLabel partialcorThreshold = new JLabel("Partial Correlation Threshold: ");
		JButton browse = new JButton("Browse");
		JButton browse1 = new JButton("Browse Me");
		JButton browse0 = new JButton ("Create Me");
		JButton run = new JButton("Run");
		JButton count = new JButton("Count");
		JPanel pan = new JPanel();
		ActionListener runlist = new RunListener();
		run.addActionListener(runlist);
		ActionListener countlist = new RunListener();
		count.addActionListener(countlist);
		ActionListener browselist = new BrowseListener(); 
		browse.addActionListener(browselist);
		browse1.addActionListener(browselist);
		browse0.addActionListener(browselist);
		pan.setLayout(new BoxLayout(pan, BoxLayout.PAGE_AXIS));
		pan.add(infilename);
		pan.add(t1);
		pan.add(browse);
		pan.add(outfilename);
		pan.add(t2);
		pan.add(browse0);
		pan.add(prevfilename);
		pan.add(t3);
		pan.add(browse1);
		
		pan.add(partialcorThreshold);
		pan.add(t4);
		pan.add(run);
		pan.add(count);
		frame.add(pan);
		frame.setSize(800, 350);
		frame.setVisible(true);	
		frame.setTitle("In-Silico Bayesian Integrator of GWAS");

		
			
			
			
	}
	
	
	
		

}
